/**
 * 
 */
package com.onewingu.gcs.client;

import java.util.ArrayList;
import java.util.List;

import com.onewingu.gcs.shared.GroupInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author er66
 *
 */
public class GroupPanel extends VerticalPanel {
	private final Grid grid;
	private List<GroupInfo> groups;
	private final Button submitButton;
	private String saUsername;
	private GContactSyncServiceAsync gcssa = null;
	private final Label statusLabel;
	
	public GroupPanel() {
		super();
		this.statusLabel = new Label();
		gcssa = (GContactSyncServiceAsync)GWT.create(GContactSyncService.class);
		this.submitButton = new Button();
		this.submitButton.setText("Submit");
		this.grid = new Grid();
		this.groups = new ArrayList<GroupInfo>();
		this.add(grid);
		this.add(submitButton);
		this.add(statusLabel);

		this.submitButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				statusLabel.setText("submitting");
				List<GroupInfo> sharedGroups = new ArrayList<GroupInfo>();
				statusLabel.setText("submitting1");
				for(int row = 0; row < grid.getRowCount(); row++) {
					statusLabel.setText("submitting1a" + row);
					//CheckBox cb = (CheckBox)grid.getWidget(row, 1);
					Widget w = grid.getWidget(row, 1);
					statusLabel.setText("w: " + w);
					
					if(w instanceof CheckBox) {
						statusLabel.setText("w is a checkbox: " + w);
					} else {
						statusLabel.setText("w is not a checkbox: " + w);
					}
					
					CheckBox cb = (CheckBox)w;
					statusLabel.setText("submitting1a" + row);
					boolean shared = cb.getValue();
					if(!shared)
						continue;

					statusLabel.setText("rrow: " + row + ". shared: " + shared + ". groups size: " + groups.size());
					GroupInfo group = groups.get(row);
					statusLabel.setText((group == null ? "group is null" : "group is not null"));
					group.setShared(shared);
					sharedGroups.add(group);
				}
				statusLabel.setText("submitting2");
				gcssa.setSharedGroups(saUsername, sharedGroups, new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						//statusLabel.setText("success");
					}
					
					@Override
					public void onFailure(Throwable caught) {
						statusLabel.setText("exception: " + caught.getMessage());
					}
				});
				statusLabel.setText("submitted");
			}
		});
	}
	
	public void setGroups(List<GroupInfo> groups) {
		if(groups == null)
			this.groups = new ArrayList<GroupInfo>();
		
		this.groups = groups;
		
		grid.resize(0, 0);
		grid.resize(groups.size(), 2);
		
		for(int row = 0; row < groups.size(); row++) {
			GroupInfo group = groups.get(row);
			CheckBox cb = new CheckBox();
			cb.setValue(group.isShared());
			grid.setText(row, 0, group.getGroupTitle());
			grid.setWidget(row, 1, cb);
		}
	}

	/**
	 * @return the saUsername
	 */
	public String getSaUsername() {
		return saUsername;
	}

	/**
	 * @param saUsername the saUsername to set
	 */
	public void setSaUsername(String saUsername) {
		this.saUsername = saUsername;
	}

	
}
